(function(root, factory) {
	if(typeof define === 'function' && define.amd) {
		// AMD. Register as an anonymous module.
		define(['exports', 'vue'], factory);
	} else if(typeof exports === 'object' && typeof exports.nodeName !== 'string') {
		// CommonJS
		factory(exports, require('vue'));
	} else {
		// Browser globals
		factory(({}), root.Vue);
	}
}(this, function(exports, Vue) {
	Vue.priceInput = {
		install: function(Vue, options) {
			var currentValue = 0,
				lastValue = null,
				inputCheckFlag = false;
			var tFocus = function() {

				inputCheckFlag = true;
				var _target = event.target;
				setTimeout(function() {
					_target.type = "text"
				}, 10);
				lastValue = event.target.value;
			}
			var tBlur = function() {
				inputCheckFlag = false;
				var _target = event.target;
				setTimeout(function() {
					_target.type = "number"
				}, 10);
			}
			var numCheck = function() {
				//匹配开头只能是0 开头 0只能是一次  或者开头是1  点只能出现一次
				var patrn = /^[0]$|^[0]{1}[\.]{1}\d{0,2}$|^[1-9]{1}[0-9]*\.?\d{0,2}$/ig;
				var patrnObj1 = new RegExp("^[1-9]{1}[0-9]*\\.?\\d{0,2}", "ig"),
					patrnObj2 = new RegExp("^[0]{1}[\\.]{1}\\d{0,2}", "ig");

				if(!patrn.test(event.target.value)) {

					var valueToNum = parseFloat(event.target.value);
					if(patrn.test(valueToNum)) { //0字开头自动变输入的数字
						this.vm[this.expression] = valueToNum;
						event.target.value = valueToNum;
					} else if(lastValue.length == 1 && event.target.value == "") { //只剩一位数字时,删除后赋值为0

						this.vm[this.expression] = 0;
						event.target.value = 0;
					} else {
						if(event.target.value.match(patrnObj1)) { //匹配1以上数值并限制多余输入
							//例如1.566变1.56
							this.vm[this.expression] = event.target.value.match(patrnObj1)[0];

							event.target.value = event.target.value.match(patrnObj1)[0];
						} else if(event.target.value.match(patrnObj2)) { //匹配1以下数值并限制多余输入
							//例如0.566变0.56						
							this.vm[this.expression] = event.target.value.match(patrnObj2)[0];
							event.target.value = event.target.value.match(patrnObj2)[0];
						}
					}
				} else {
					this.vm[this.expression] = event.target.value;
				}
				lastValue = event.target.value;
			}
			var updateNum = function(value) {

				if(!inputCheckFlag) { //非输入状态下值变化检测

					this.vm[this.expression] = value;
					var patrn = /^-?(\d+|\d+\.\d+|\.\d+)([eE][-+]?\d+)?$/ig;
					if(patrn.test(this.vm[this.expression])) {
						this.el.value = this.vm[this.expression];
					} else {
						this.el.value = "0";
						console.warn("priceInput: 检测到值为非数字类型");
					}
					lastValue = value;
				}
			}

			var definition = {
				twoWay: true,
				bind: function() {
					this.el.value = this.vm[this.expression];
					this.unFocus = frameScript.addEventListener(this.el, "focus", tFocus);
					this.unBlur = frameScript.addEventListener(this.el, "blur", tBlur);
					this.unInput = frameScript.addEventListener(this.el, "input", numCheck.bind(this));
				},
				update: function(value, oldValue) {
					//这里的update 紧跟bind的值 value 新值  oldValue 老值
					console.log(value + '||', oldValue)
						//调用回调方法
					updateNum.call(this, value);
				},
				unbind: function() {
					//解绑相关方法
					this.unFocus();
					this.unBlur();
					this.unInput();
				},
			}
			Vue.directive('priceinput', definition);
		}
	}
}));